package com.luke.exercise.aop;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class DynamicProxy2 implements InvocationHandler {

    public Object target;
    
    public DynamicProxy2(Object target) {
        this.target = target;
    }
    
    @SuppressWarnings("unchecked")
    public <T>T getProxy(){
        return (T)Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }
    
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        // TODO Auto-generated method stub
        before();
        Object result = method.invoke(target, args);
        after();
        return result;
    }
    
    private void before() {
        System.out.println("before");
    }
    
    private void after() {
        System.out.println("after");
    }
    
    public static void main(String[] args) {
        DynamicProxy2 proxy = new DynamicProxy2(new HelloImpl());
        Hello1 hello = proxy.getProxy();
        hello.say("luke");
    }

}
